<?php
function list_komponen($periode_id){
	$periode_gaji_id=get_periode_gaji() ;
	if($periode_id==0){
		$periode_id=$periode_gaji_id;
	}	
	if( $periode_gaji_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}
	$tipes = array(
		'1'=>'Tunjangan',
		'2'=>'Potongan',
		'3'=>'Iuran',
	);	

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Tipe'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		 
	);

	$query = "SELECT komponen_gaji_id, nama_komponen_gaji, tipe 
			FROM pr_komponen_gaji
			ORDER BY tipe, komponen_gaji_id"
	;
	
	$result = my_query($query);
	$total_records = my_num_rows($result ) + 1;
	$scroll_page =5;  
	$per_page =25;  
	$last_page_number = ceil($total_records/$per_page)  ;
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&periode_id={$_GET['periode_id']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
 
	while($ey = my_fetch_array($result)){
		$komponen_id=$ey['komponen_gaji_id'];
		$query = "SELECT SUM(kalkulasi) as kalkulasi 
			FROM pr_gaji_komponen  
			WHERE komponen_id={$komponen_id}
			AND periode_id={$kalkulasi_periode}" ;
		$res = my_fetch_array(my_query($query));
		$i++;
		$row[] = array(
			'no'=>position_text_align($i,'center'),  
			'gd'=>  $ey['nama_komponen_gaji'] ,
			'tipe'=>$tipes[$ey['tipe']],
			'bulan_ini'=>  position_text_align (rp_format($res['kalkulasi']) ,'right'),      
		); 
	}
	if($last_page_number == $_GET['page']){
	
	$query_gaji = "SELECT SUM(gaji_dibayar) as kalkulasi 
	FROM pr_gaji   
	WHERE periode_id={$kalkulasi_periode} ";
	$resultgaji2 = my_query($query_gaji);
	$resgaji2 = my_fetch_array($resultgaji2); 
	$i++;
	$row[] = array(
		'no'=>position_text_align($i,'center'),  
		'gd'=>  'Gaji dibayarkan',
		'tipe'=>'Cash paid',
		'bulan_ini'=>   position_text_align (rp_format( $resgaji2['kalkulasi'])   ,'right')  
	);
	
	 $i++;
	$queryw = "SELECT SUM(takehomepay) as kalkulasi 
	FROM pr_gaji   
	WHERE periode_id={$kalkulasi_periode}" ;
	$resultxw = my_query($queryw);
	$resw = my_fetch_array($resultxw); 
	$row[] = array(
		'no'=>position_text_align($i,'center'),  
		'gd'=>  'Take homepay',
		'tipe'=>'Cash paid',
		'bulan_ini'=>  position_text_align (rp_format($resw['kalkulasi']) ,'right')  
	);
 
	}
	$datas = table_rows($row);
	my_set_code_js(' 
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&periode_id=\'+periode_id ;
		}
	');

	$query_check_periode = " SELECT * FROM pr_periode where periode_id <= {$periode_gaji_id} ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$form_dpwerio = form_dropdown($fperiode_id,$prs);
	$paging = $kgPagerOBJ ->showPaging();	
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.$form_dpwerio.'</span>'   , "<b>Periode Gaji</b>"    ); 
	$menuontop = array(  
		'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com=rpt_komponen_data&task=export_excel&periode_id='.$periode_id.'\''),
		'Cetak' =>array('onclick'=>'javascript:window.open(\'index.php?com=report&task=rpt_komponen_data&periode_id='.$periode_id.'&ip='.rand(0,10000).'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $view.$printmenu.table_builder($header , $datas ,  6 , false , $paging  );
}


function list_komponen_excel($periode_id){
	$periode_gaji_id=get_periode_gaji() ;
	if($periode_id==0){
		$periode_id=$periode_gaji_id;
	}	
	if( $periode_gaji_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}

	$tipes = array(
		'1'=>'Tunjangan',
		'2'=>'Potongan',
		'3'=>'Iuran',
	);	

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama Komponen'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Tipe'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  		 
	);

	$query = "SELECT komponen_gaji_id, nama_komponen_gaji, tipe 
			FROM pr_komponen_gaji
			ORDER BY tipe, komponen_gaji_id"
	;
	
	$result = my_query($query);

	while($ey = my_fetch_array($result)){
		$komponen_id=$ey['komponen_gaji_id'];
		$query = "SELECT SUM(kalkulasi) as kalkulasi 
			FROM pr_gaji_komponen  
			WHERE komponen_id={$komponen_id}
			AND periode_id={$kalkulasi_periode}" ;
		$res = my_fetch_array(my_query($query));
		$i++;
		$row[] = array(
			'no'=>position_text_align($i,'center'),  
			'gd'=>  $ey['nama_komponen_gaji'] ,
			'tipe'=>$tipes[$ey['tipe']],
			'bulan_ini'=>   $res['kalkulasi'],      
		); 
	}
	
	$query_gaji =  "SELECT SUM(gaji_dibayar) as kalkulasi 
	FROM pr_gaji   
	WHERE periode_id={$kalkulasi_periode} ";
	$resultgaji2 = my_query($query_gaji);
	$resgaji2 = my_fetch_array($resultgaji2); 
	$i++;
	$row[] = array(
		'no'=>position_text_align($i,'center'),  
		'gd'=>  'Gaji dibayarkan',
		'tipe'=>'Cash paid',
		'bulan_ini'=>   $resgaji2['kalkulasi']  
	);
	
	$i++;
	$queryw = "SELECT SUM(takehomepay) as kalkulasi 
	FROM pr_gaji   
	WHERE periode_id={$kalkulasi_periode}" ;
	$resultxw = my_query($queryw);
	$resw = my_fetch_array($resultxw); 
	$row[] = array(
		'no'=>position_text_align($i,'center'),  
		'gd'=>  'Take homepay',
		'tipe'=>'Cash paid',
		'bulan_ini'=>   $resw['kalkulasi']  
	);
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 6 ,false ); 
}